package com.gan.springbootdemo.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 * create by admin 2018/06/29
 */
@Slf4j
public final class SessionRedis {

    private SessionRedis() {
    }

    /**
     * 获取session用户
     *
     * @param request       request
     * @param redisTemplate redisTemplate
     * @return StmUsr
     */
    public static String getUsr(HttpServletRequest request, RedisTemplate<String, String> redisTemplate) {
        try {
            String token = request.getHeader(Constant.AUTHORIZATION);
            log.info("token = {}", token);

            String usrString = redisTemplate.opsForValue().get(token);
            log.info("usrString = {}", usrString);

            if (usrString != null && !usrString.isEmpty()) {
                return usrString;
            }
        } catch (Exception e) {
            log.error("getUsr", e);
        }
        return null;
    }
}
